<template>
	<view>
		<view class="navbar" style="padding: 20rpx;background-color: #fff;height: 180rpx;">
			<view class="title" style="text-align: center;font-size: 45rpx;color: #dd6161;font-weight: bold;">首页</view>
			<view class="search" style="width: 95%;height: 64rpx;border-radius: 50rpx;border: 2rpx solid #dd6161;
			background-color: #f2f2f2;margin: 20rpx auto;display: flex;flex-direction: row;justify-content: start;
			align-items: center;position: relative;flex-shrink: 0;">
				<u-icon name="search" style="margin-left: 20rpx;"></u-icon>
				<input type="text" placeholder="请输入关键字" style="margin-left: 20rpx;" v-model="value">
				<button class="bu" @click="submit" style="width: 120rpx;height: 95%;border-radius: 50rpx;position: absolute;
				right: 1rpx;top: 1rpx;background-color: #fa3534;">搜索</button>
			</view>
		</view>
		<scroll-view scroll-y="true" class="main">
			<view class="content">
				<view class="swiper" style="padding: 20rpx;background-color: #fff;">
					<u-swiper :list="swiperList" @click="to" height="300"></u-swiper>
				</view>
				<view class="cate">
					<navigator url="" class="cate-item center" v-for="item in cateList">
						<image :src="$baseUrl + item.imgUrl"></image>
						<text>{{item.serviceName}}</text>
					</navigator>
					<navigator url="" class="cate-item center" @click="toService">
						<u-icon name="list-dot" size="80" class="icon center"></u-icon>
						<text>更多服务</text>
					</navigator>
				</view>
				<view class="hot">
					<view class="title1">热门新闻</view>
					<view class="hot-list">
						<view class="hot-item center" v-for="item in hotList">
							<navigator :url="`/pages/news-detail/news-detail?id=${item.id}`">
								<image :src="$baseUrl + item.cover"></image>
							</navigator>
							<text class="line">{{item.title}}</text>
						</view>
					</view>
				</view>
				<view class="news" style="margin-top: 20rpx;">
					<u-sticky offset-top="90">
						<u-tabs :list="tabList" :is-scroll="true" :current="tabCurrent" @change="tabChange"></u-tabs>
					</u-sticky>
					<view class="news-list" style="padding: 20rpx;background-color: #fff;">
						<view class="news-item" v-for="item in newsList" style="padding: 20rpx 0;border-top: 2rpx solid #e4e7ed;
						display: flex;flex-direction: row;justify-content: space-between;align-items: center;">
							<view class="left" style="width: 450rpx;">
								<view class="top" style="height: 168rpx;">
									<text class="line" style="font-weight: bold;font-size: 32rpx;">{{item.title}}</text>
									<rich-text :nodes="item.content" class="line" style="font-size: 26rpx;"></rich-text>
								</view>
								<view class="bottom" style="display: flex;flex-direction: row;color: #666;font-size: 22rpx;">
									<text>{{item.readNum}} 阅读</text>
									<text style="margin-left: 20rpx;">{{item.publishDate}}</text>
								</view>
							</view>
							<view class="right center">
								<navigator :url="`/pages/news-detail/news-detail?id=${item.id}`">
									<image :src="$baseUrl + item.cover" mode="" style="width: 250rpx;height: 200rpx;"></image>
								</navigator>
							</view>
						</view>
					</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				value: '',
				swiperList: [],
				cateList: [],
				hotList: [],
				tabList: [],
				tabCurrent: 0,
				newsList: []
			}
		},
		onLoad() {
			if(!uni.getStorageSync('token')){
				uni.reLaunch({
					url:'/pages/login/login'
				})
			}
			this.getSwiper()
			this.getCate()
			this.getHot()
			this.getTab()
		},
		methods: {
			submit(){
				uni.navigateTo({
					url:'/pages/search/search?key=' + this.value
				})
			},
			getSwiper(){
				this.$request({
					url: '/prod-api/api/rotation/list',
					data: {
						type: '2'
					}
				}).then(res => {
					console.log(res)
					this.swiperList = res.rows.map(item => ({image: this.$baseUrl + item.advImg, id: item.targetId}))
				})
			},
			to(index){
				uni.navigateTo({
					url:'/pages/news-detail/news-detail?id=' + this.swiperList[index].id
				})
			},
			getCate(){
				this.$request({
					url: '/prod-api/api/service/list'
				}).then(res => {
					console.log(res)
					this.cateList = res.rows.splice(0,9)
				})
			},
			getHot(){
				this.$request({
					url: '/prod-api/press/press/list',
					data: {
						hot: 'Y'
					}
				}).then(res => {
					console.log(res)
					this.hotList = res.rows
				})
			},
			getTab(){
				this.$request({
					url: '/prod-api/press/category/list'
				}).then(res => {
					console.log(res)
					this.tabList = res.data
					this.getNews()
				})
			},
			tabChange(index){
				this.tabCurrent = index
				this.getNews()
			},
			getNews(){
				this.$request({
					url: '/prod-api/press/press/list',
					data:{
						type: this.tabList[this.tabCurrent].id
					}
				}).then(res => {
					this.newsList = res.rows
				})
			}
		}
	}
</script>

<style lang="scss">
	page{
		height: 100%;
		view{
			height: 100%;
			display: flex;
			flex-direction: column;
			.main{
				flex: 1;
				overflow: hidden;
				.content{
					height: fit-content;
					background-color: #f7f7f7;
					.cate{
						padding: 20rpx;
						display: flex;
						flex-direction: row;
						flex-wrap: wrap;
						justify-content: space-between;
						align-items: center;
						.cate-item{
							width: 120rpx;
							padding: 20rpx 0;
							height: fit-content;
							display: flex;
							flex-direction: column;
							image{
								width: 100rpx;
								height: 100rpx;
							}
							text{
								width: 120rpx;
								text-align: center;
								white-space: nowrap;
								overflow: hidden;
								text-overflow: ellipsis;
								color: #666;
								margin-top: 10rpx;
							}
							.icon{
								width: 100rpx;
								height: 100rpx;
								border: 4rpx solid #2979ff;
								border-radius: 50%;
								color: #2979ff;
							}
						}
					}
					.hot{
						padding: 20rpx;
						background-color: #fff;
						.hot-list{
							display: flex;
							flex-direction: row;
							justify-content: space-between;
							align-items: center;
							padding: 20rpx 0;
							border-top: 2rpx solid #e4e7ed;
							.hot-item{
								flex: 1;
								display: flex;
								flex-direction: column;
								image{
									width: 300rpx;
									height: 250rpx;
									border-radius: 20rpx;
								}
								text{
									width: 300rpx;
									font-weight: bold;
								}
							}
						}
					}
				}
			}
		}
	}
</style>
